package com.struct.graph.loader
{
	import com.collection.ActionCollection;
	
	import flash.display.DisplayObjectContainer;
	import flash.utils.Dictionary;
	
	import flashx.textLayout.elements.Configuration;
	
	import sacu.s.FactoryEvent;
	import sacu.s.worker.GraphWorker;
	
	public class LoaderGraphWorker extends GraphWorker
	{
		//状态
		private var state:Boolean;
		//
		private var _base:LoaderFrame;
		//通过配置监听获取设置
		/**
		 * 每个加载之前都会第一次发送加载设置。
		 * 
		 */
		private var config:Array;
		private var total:uint;//总条数
		private var current:uint;//计数器
		private var validator:String;//验证器
		public function LoaderGraphWorker(id:String, base:DisplayObjectContainer)
		{
			super(id, base);
			_base = base as LoaderFrame;
			config = [];
			addEventDispatcher(ActionCollection.CONFIG_LOADER);
			addEventDispatcher(ActionCollection.DATA_LOADER);
		}
		override public function handleAction(action:FactoryEvent):void{
			var c:ConfigValidatorVO;
			switch(action.type){
				case ActionCollection.CONFIG_LOADER:{
					c = new ConfigValidatorVO(action.body);
					var index:int = -1;
					for(var i:uint = 0; i < config.length; ++i){
						if(config[i].validator == c.validator){
							index = i;
							break;
						}
					}
					if(c.destroy){
						config.splice(i, 1);
					} else if(state){//工作中
						if(index == -1){
							config.push(c);
						} else {
							config[i] = c;
						}
					} else {//闲暇
						setCondition(c);
					}
					break;
				}
				case ActionCollection.DATA_LOADER:{
					var v:DataValidatorVO = new DataValidatorVO(action.body);
					if(v.validator == validator){
						if(++current == total){//加载完毕
							if(config.length > 0){//总加载是否完毕
								setCondition(config.shift() as ConfigValidatorVO);
							} else {//全部加载完毕
								factory.disposeFactory();
							}
						} else {//显示加载信息
							_base.setShowInfo(v.name, Number(current / total * 100).toFixed(2) + "%");
						}
					}
					break;
				}
			}
		}
		private function setCondition(c:ConfigValidatorVO):void{
			state = true;
			total = c.total;
			current = 0;
			validator = c.validator;
			_base.setShowInfo(c.name, "0%");
		}
		public function dispose():void{
			state = false;
			for(var validator:String in config){
				delete config[validator];
			}
		}
	}
}